import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'// progress bar style
import { getToken,removeToken } from '@/utils/auth' // getToken from cookie
import toast from './utils/toast'
let users = [
  'upload_thtd',
  'upload_thtd2',
  'upload_thtd3',
  'upload_thtd4',
  'upload_rj',
  'upload_rj2',
  'upload_rj3',
  'upload_rj4',
  'mcn_1',
  'weihu1',
  'bianji_1'
]
let noAudit = [
  'yunyin_2',
  'yunyin_3',
  'shenhe_2',
  'shenhe_3',
  'shenhe_4',
]
NProgress.configure({ showSpinner: false })// NProgress Configuration

// permission judge function
// function hasPermission(perms, permissions) {
//   if (perms.indexOf('*') >= 0) return true // admin permission passed directly
//   if (!permissions) return true
//   return perms.some(perm => permissions.indexOf(perm) >= 0)
// }

const whiteList = ['/login', '/auth-redirect']// no redirect whitelist

router.beforeEach((to, from, next) => {
  console.log(getToken(),'11')
  
  // console.log('beforeEach', to)
  NProgress.start() // start progress bar
  if (getToken()) { // determine if there has token
    /* has token*/
    console.log(to.path,'path')
    if (to.path === '/login') {
      // router.addRoutes(store.getters.addRouters)
      next({ path: '/' })  
      NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
    } 
    else {
      if (store.getters.perms.length === 0) { // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetUserInfo').then(res => { // 拉取user_info
          if(res.data.resp_code == 0){
            const perms = res.data.datas.perms // note: perms must be a array! such as: ['GET /aaa','POST /bbb']
            console.log(perms,'perms')
            store.dispatch('getroutes').then(()=>{
              router.addRoutes(store.getters.addRouters)
              next({ ...to, replace: true })
            })
          }else{
            removeToken()
            store.commit("SET_TOKEN", '')
            toast.fail('请重新登录')
            next(`/login?redirect=${to.fullPath}`)
            NProgress.done()
          }
        //   store.dispatch('GenerateRoutes', { perms }).then(() => { // 根据perms权限生成可访问的路由表
        //     router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
        //     next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
        //   })
        // }).catch((err) => {
        //   store.dispatch('FedLogOut').then(() => {
        //     Message.error(err || 'Verification failed, please login again')
        //     next({ path: '/' })
        //   })
        })
      } else {
        if(store.state.user.name == 'test1'){
          if(to.path=='/radio/videoList'||to.path=='/datasView'||to.path=='/'){
            next()
          }else{
            NProgress.done()
            return toast.fail('暂无权限查看')
          }
        }else if(users.includes(store.state.user.name)){
          if(to.path=='/radio/videoList' || to.path=='/radio/batchUpdate'||to.path=='/datasView'||to.path=='/'){
            next()
          }else{
            NProgress.done()
            return toast.fail('暂无权限查看')
          }
        }else if(noAudit.includes(store.state.user.name)){
          if(to.path != '/radio/examine'){
            next()
          }else{
            NProgress.done()
            return toast.fail('暂无权限查看')
          }
        }else if(store.state.user.name === 'activity_mcn'){
          if(to.path == '/radio/examine'){
            next()
          }else{
            NProgress.done()
            return toast.fail('暂无权限查看')
          }
        }else{
          next()
        }
        // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
        // if (hasPermission(store.getters.perms, to.meta.perms)) {
          // next()
          // next({path:'/'})
        // } else {
        //   next({ path: '/401', replace: true, query: { noGoBack: true }})
        // }
        // 可删 ↑
      }
    }
  } else {
    /* has no token*/
    console.log('退出了')
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
      next()
    } else {
      store.commit("SET_TOKEN", '')
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})
